package Presentation;

import BusinessLogic.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

public class frmLogin extends javax.swing.JFrame {

    public static int x;
    public static int y;
    static frmRegister register;
    String ServerIP;
    String State = Command.ONLINE;
    Connection con = new Connection();
    String path;
    public frmLogin() {
        try {
            setIcon();
            Thread.sleep(1000);
            initComponents();
            Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
            x = this.getWidth();
            y = this.getHeight();
            motion();
            txt_Username.requestFocus();
            path= System.getProperty("java.io.tmpdir")+"chatconfig";
        } catch (InterruptedException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void motion() {
        this.setSize(0, 0);
        new Thread(new SizeMonition(this, x, y)).start();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        btn_Register = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txt_Password = new javax.swing.JPasswordField();
        btn_Login = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txt_Username = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btn_Clear = new javax.swing.JButton();
        cb_Remember = new javax.swing.JCheckBox();
        cb_Invisible = new javax.swing.JCheckBox();
        mnbar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mni_IP = new javax.swing.JMenuItem();
        mni_Exit = new javax.swing.JMenuItem();

        jLabel5.setText("jLabel5");

        setTitle("Đăng nhập");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(204, 204, 0));
        setIconImage(getIconImage());
        setName("frm_dangnhap"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/key.png"))); // NOI18N

        jPanel1.setBackground(new java.awt.Color(0, 153, 153));

        jLabel3.setFont(new java.awt.Font("Times New Roman", 2, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nếu chưa có tài khoản, hãy đăng ký tại đây: ");
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        btn_Register.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/next.png"))); // NOI18N
        btn_Register.setBorder(null);
        btn_Register.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Register.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_Register.setName("btn_dangky"); // NOI18N
        btn_Register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_RegisterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_Register, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btn_Register, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 51));
        jLabel6.setText("G_IT");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Thông tin đăng nhập", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 51, 255))); // NOI18N

        txt_Password.setMinimumSize(new java.awt.Dimension(4, 20));
        txt_Password.setName("txt_Password"); // NOI18N
        txt_Password.setPreferredSize(new java.awt.Dimension(4, 20));
        txt_Password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_PasswordKeyPressed(evt);
            }
        });

        btn_Login.setBackground(new java.awt.Color(204, 204, 255));
        btn_Login.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btn_Login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/accept.png"))); // NOI18N
        btn_Login.setText("Đăng nhập");
        btn_Login.setBorder(null);
        btn_Login.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_LoginActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel2.setText("Mật khẩu");

        txt_Username.setName("txt_Username"); // NOI18N
        txt_Username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_UsernameActionPerformed(evt);
            }
        });
        txt_Username.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_UsernameKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel1.setText("Tên đăng nhập");

        btn_Clear.setBackground(new java.awt.Color(204, 204, 255));
        btn_Clear.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btn_Clear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete_friend.png"))); // NOI18N
        btn_Clear.setText("Hủy Bỏ");
        btn_Clear.setBorder(null);
        btn_Clear.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ClearActionPerformed(evt);
            }
        });

        cb_Remember.setFont(new java.awt.Font("Times New Roman", 3, 12)); // NOI18N
        cb_Remember.setForeground(new java.awt.Color(0, 51, 255));
        cb_Remember.setText("Ghi nhớ đăng nhập");

        cb_Invisible.setFont(new java.awt.Font("Times New Roman", 3, 12)); // NOI18N
        cb_Invisible.setForeground(new java.awt.Color(0, 51, 255));
        cb_Invisible.setText("Đăng nhập ẩn");
        cb_Invisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_InvisibleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(cb_Remember)
                                .addGap(18, 18, 18)
                                .addComponent(cb_Invisible))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txt_Username, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                                    .addComponent(txt_Password, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(btn_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_Clear, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_Password, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cb_Remember)
                    .addComponent(cb_Invisible))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Clear, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        txt_Username.getAccessibleContext().setAccessibleName("");

        mnbar.setBackground(new java.awt.Color(153, 204, 255));
        mnbar.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jMenu1.setText("Tùy chỉnh");

        mni_IP.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        mni_IP.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/config.png"))); // NOI18N
        mni_IP.setText("Kết nối");
        mni_IP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_IPActionPerformed(evt);
            }
        });
        jMenu1.add(mni_IP);

        mni_Exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        mni_Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/exit.png"))); // NOI18N
        mni_Exit.setText("Thoát");
        mni_Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_ExitActionPerformed(evt);
            }
        });
        jMenu1.add(mni_Exit);

        mnbar.add(jMenu1);

        setJMenuBar(mnbar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addComponent(jLabel6)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void txt_UsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_UsernameActionPerformed

    }//GEN-LAST:event_txt_UsernameActionPerformed
    private void btn_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_LoginActionPerformed
        if (!txt_Username.getText().equals("") && !txt_Password.getText().equals("")) {
            if (!con.connect(ServerIP)) {
                JOptionPane.showMessageDialog(rootPane, "Lỗi! Không thể kết nối đến server", "Lỗi kết nối", JOptionPane.ERROR_MESSAGE);
            } else {
                String[] arrayInfor = new String[4];
                arrayInfor[0] = "checkdata";//dong thu 1
                arrayInfor[1] = Encode.encode_url_v3(txt_Username.getText(), "G_IT");//dong thu 2 (tai khoan)
                arrayInfor[2] = Encode.encode_url_v3(txt_Password.getText(), "G_IT");//dong thu 3 (mat khau) 

                if (cb_Remember.isSelected()) {
                    arrayInfor[3] = "True";
                } else {
                    arrayInfor[3] = "False";//ghi dong thu 4 (true/false) co duoc ghi nho?
                }
                FileProcess writenfile = new FileProcess(path, null, 0, arrayInfor);
                writenfile.WriteFile();//tien hanh ghi mang arrayInfor vao file config.
                
                String str = Command.LOGIN_REQUEST + State + txt_Username.getText() + ((char) 001) + txt_Password.getText();
                sendData(str);
            }
        } else if (txt_Username.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Tên đăng nhập không được rỗng!", "Lỗi đăng nhập", JOptionPane.ERROR_MESSAGE);
            txt_Username.requestFocus();
        } else {
            JOptionPane.showMessageDialog(null, "Mật khẩu không được rỗng!", "Lỗi đăng nhập", JOptionPane.ERROR_MESSAGE);
            txt_Password.requestFocus();
        }
    }//GEN-LAST:event_btn_LoginActionPerformed

    private void btn_ClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ClearActionPerformed
        clear();
    }//GEN-LAST:event_btn_ClearActionPerformed

    private void btn_RegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_RegisterActionPerformed
        if (!con.connect(ServerIP)) {
            JOptionPane.showMessageDialog(rootPane, "Lỗi! Không thể kết nối đến server", "Lỗi kết nối", JOptionPane.ERROR_MESSAGE);
        } else {
            register = new frmRegister();
            register.setVisible(true);
        }
    }//GEN-LAST:event_btn_RegisterActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            con.exitProgram();
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_formWindowClosing

private void txt_UsernameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_UsernameKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        txt_Password.requestFocusInWindow();
        txt_Password.selectAll();
    }
}//GEN-LAST:event_txt_UsernameKeyPressed

private void txt_PasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_PasswordKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        btn_LoginActionPerformed(null);
    }
}//GEN-LAST:event_txt_PasswordKeyPressed

private void mni_ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_ExitActionPerformed
    try {
        con.exitProgram();
    } catch (IOException ex) {
    }
}//GEN-LAST:event_mni_ExitActionPerformed
//Sửa kết nối
private void mni_IPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_IPActionPerformed
    ServerIP = (String) JOptionPane.showInputDialog(this, "Nhập địa chỉ IP máy chủ", "Thiết lập kết nối với máy chủ",
            JOptionPane.PLAIN_MESSAGE, null, null, ServerIP);
    
    String[] arr = {"checkip",ServerIP,"True"};
    FileProcess writenfile = new FileProcess(path+"ip", null, 0, arr);
    writenfile.WriteFile();
}//GEN-LAST:event_mni_IPActionPerformed

    private void cb_InvisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_InvisibleActionPerformed
        State = Command.OFFLINE;
    }//GEN-LAST:event_cb_InvisibleActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        File ip = new File(path+"ip");
        if(!ip.exists()){
            FileOutputStream os;
            try {
                os = new FileOutputStream(ip);
                PrintWriter w = new PrintWriter(os, true);
                w.println("checkip");
                w.println("172.16.22.70"); //Địa chỉ ip mặc định của server
                w.println("True");
            } catch (Exception ex) {
            
            }           
        }
        FileProcess openip = new FileProcess(path+"ip","checkip",1,null);
        ServerIP = openip.OpenFile()[0];
        if(ServerIP.equals("")) ServerIP="172.16.22.70";  //Địa chỉ ip mặc định của server
        
        File f = new File(path);
        if(!f.exists()){
            FileOutputStream os;
            try {
                os = new FileOutputStream(f);
                PrintWriter w = new PrintWriter(os, true);
                w.println("checkdata");
                w.println("G_IT");
                w.println("G_IT");
                w.println("False");
            } catch (Exception ex) {
            
            }           
        }
        FileProcess opennfile = new FileProcess(path, "checkdata", 3, null);
        String username = Encode.decode_url_v3(opennfile.OpenFile()[0]);
        String password = Encode.decode_url_v3(opennfile.OpenFile()[1]);
        //--------------------------------------------------------------------
        String flag = opennfile.OpenFile()[2];
        //--------------------------------------------------------------------
        if ("True".equals(flag)) {
            txt_Username.setText(username);
            txt_Password.setText(password);
            cb_Remember.setSelected(true);
        } else {
            cb_Remember.setSelected(false);
        }
        
    }//GEN-LAST:event_formWindowOpened

    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/icon.png")));
    }

    private void clear() {
        txt_Password.setText(null);
        txt_Username.setText(null);
        txt_Username.requestFocus();
    }

    public static void closeRegister() {
        register.setVisible(false);
    }

    private void sendData(String str) {
        if (!Connection.sendData(str)) {
            JOptionPane.showMessageDialog(rootPane, "Lỗi! Mất kết nối với server", "Lỗi kết nối", JOptionPane.ERROR_MESSAGE);
                Connection.reConnect();       
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Clear;
    private javax.swing.JButton btn_Login;
    private javax.swing.JButton btn_Register;
    private javax.swing.JCheckBox cb_Invisible;
    private javax.swing.JCheckBox cb_Remember;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JMenuBar mnbar;
    private javax.swing.JMenuItem mni_Exit;
    private javax.swing.JMenuItem mni_IP;
    private javax.swing.JPasswordField txt_Password;
    private javax.swing.JTextField txt_Username;
    // End of variables declaration//GEN-END:variables
}
